package Tools;

import Model.Student;
import Model.userpw;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Random;

public class MyTool {
    private Connection connection1;
 public static ArrayList<userpw> userpws;


    public MyTool() throws Exception {
        userpws = new ArrayList<>();
        Properties properties = new Properties();
        FileInputStream inStream = new FileInputStream("src/student.properties");
        properties.load(inStream);
//        DruidDataSource.createDataSource(properties);
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        connection1 = dataSource.getConnection();

        String sql = "select * from userpw";
        PreparedStatement statement = connection1.prepareStatement(sql);
        ResultSet resultSet = statement.executeQuery();

        while (resultSet.next()) {
            userpw userpw = new userpw();
            userpw.setId(resultSet.getInt("id"));
            userpw.setUser(resultSet.getString("user"));
            userpw.setPw(resultSet.getString("pw"));
            userpws.add(userpw);
        }


    }
    public static String code() {
        String res = "";
        char[] str = {'1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'A', 'b', 'B', 'c', 'd', 'F'};
        Random random = new Random();

        for (int i = 0; i < 4; i++) {
            int anInt = random.nextInt(16);
            res += str[anInt];
        }
        return res;
    }

}
